const VAL = [
  0, Infinity, NaN, Number.MIN_VALUE, Number.MAX_VALUE
].flatMap(x => [x, -x])

const view = new DataView(new ArrayBuffer(8))

function rand() {
  const idx = 0 | VAL.length * 5 * Math.random()
  if (idx < VAL.length) return VAL[idx]

  view.setUint32(0, Math.random() * 4294967296)
  view.setUint32(4, Math.random() * 4294967296)
  return view.getFloat64(0)
}

self.onmessage = ({ data }) => {
  const [args, expr]: [string[], string] = data
  const fn = new Function(...args, `return ${expr}`)
  let count = 0
  setInterval(() => {
    const to = count + 100
    while (count++, count < to) {
      const arg: number[] = []
      for (let i = 0; i < args.length; i++) arg.push(rand())
      if (!fn(...arg)) {
        self.postMessage({ type: 'fail', arg, count })
        self.close()
        return
      }
    }
    self.postMessage({ type: 'ok', count })
  }, 0)
}